package com.ea.framework.filter;



import com.ea.util.HttpContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		httpServletResponse.setContentType("application/json;charset=utf-8");
		//httpServletResponse.setHeader("Access-Control-Allow-Origin","*");
		httpServletResponse.setHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
		httpServletResponse.setHeader("Access-Control-Allow-Headers", "Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,token,x-access-token");
		httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
		httpServletResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());
		httpServletResponse.setHeader("Access-Control-Max-Age", "1728000");
		String requestURL = httpServletRequest.getRequestURL().toString();
		System.out.println("请求地址：" + requestURL);
		Long startTime = System.currentTimeMillis();
		try{
			chain.doFilter(request, response);
		}catch (Exception e){
			e.printStackTrace();
			throw  e;
		}finally {
			Long endTime = System.currentTimeMillis() - startTime;
			System.err.println("请求地址：" + requestURL+",请求时间:"+endTime);
		}

	}

	@Override
	public void destroy() {

	}

}
